

//https://www.nowcoder.com/practice/7a0da8fc483247ff8800059e12d7caf1?tpId=13&tqId=23248&ru=/exam/oj/ta&qru=/ta/coding-interviews/question-ranking&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D13

//思想1:传统的递归实现；
//思想2:虚函数实现，其实也是在递归；
//思想3:构造函数实现 + new数组；
//思想4:模版元编程

#include <limits>
#include <vector>
#include <math.h>

using namespace std;

class Solution {
public:
    int Sum_Solution(int n) {
        (n > 1)&& (n += Sum_Solution(n - 1));
        return n;
    }
};


class A;//声明类A
A* Arr[2];
class A {
  public:
    virtual int Sum(int n) {
        return 0;
    }
};
class B: public A {
  public:
    virtual int Sum(int n) {
        return Arr[(bool)n]->Sum(n - 1) + n; //bool类型转换;
        //return Arr[!!n]->Sum(n-1)+n;也可以使用两次取反操作；
    }
};
class Solution {
  public:
    int Sum_Solution(int n) {
        A a;
        B b;
        Arr[0] = &a;
        Arr[1] = &b;
        return Arr[1]->Sum(n);
    }
};


class A {
  public:
    A() {
        ++N;
        sum += N;
    }
    static void Reset() {
        N = 0;
        sum = 0;
    }
    static int N;
    static int sum;
};
int A::N = 0;
int A::sum = 0;
class Solution {
  public:
    int Sum_Solution(int n) {
        A::Reset();
        A* a = new A[n];
        delete []a;//防止内存泄漏
        a = nullptr; //防止悬空指针
        return A::sum;
    }
};

template<int n> struct Sum{
    enum value{N=Sum<n-1>::N+n};
};
template<> struct Sum<1>{
    enum value{N=1};
};
template<> struct Sum<0>{
    enum value{N=0};
};

